package com.mx.zb.mng.ctrl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

import com.mx.zb.mng.bo.BizIndustry;
import com.mx.zb.mng.bo.Note;
import com.mx.zb.mng.bo.Template;
import com.mx.zb.mng.bo.Tenant;
import com.mx.zb.mng.dao.BizIndustryMapper;
import com.mx.zb.mng.dao.NoteMapper;
import com.mx.zb.mng.dao.TemplateMapper;
import com.mx.zb.mng.dao.TenantMapper;
import com.mx.zb.mng.utils.UnionCode;
import com.mx.zb.mng.utils.context.ContextThread;
import com.mx.zb.mng.utils.ex.PageAnnotation;
import com.mx.zb.mng.utils.ex.ServiceException;

import net.minidev.json.JSONObject;

@Controller
@PageAnnotation
@RequestMapping("/p/biz")
public class TemplateCtrl {
	@Autowired
	public TemplateMapper templateMapper;
	@Autowired
	BizIndustryMapper bizIndustryMapper;
	
	@RequestMapping(value = { "/tmp" })
	public String toTemplateList(Model model) {
		List<BizIndustry> tmpList = bizIndustryMapper.selectTemplates();
		model.addAttribute("tpList", tmpList);
		return "templateList";
	}

	@RequestMapping(value = { "/tmp/toTemplate" })
	public String toaddTemplate(Model model) {
		
		return "addTemplate";
	}
	
	@RequestMapping(value = { "/tmp/addTemplate" })
	@Transactional
	public String addTemplate(Template t,Model model) {
		t.setCreateDate(new Date());
		t.setCreateOper(ContextThread.get().getOperatorId());
		templateMapper.insert(t);
		List<BizIndustry> tmpList = bizIndustryMapper.selectTemplates();
		model.addAttribute("tpList", tmpList);
		return "templateList";
	}
	
	@RequestMapping(value = { "/tmp/show" })
	public String showTemplate(@RequestParam int id, Model model) {
		Template o = templateMapper.selectByPrimaryKey(id);
		
		model.addAttribute("temp", o);
		return "templateEdit";
	}
	
	
	@RequestMapping(value = { "/tmp/edit" })
	@Transactional
	public String editTemplate( Template t,Model model) {
		t.setUpdateDate(new Date());
		t.setUpdateOper(ContextThread.get().getOperatorId());
		  templateMapper.updateByPrimaryKeySelective(t);
		  List<BizIndustry> tmpList = bizIndustryMapper.selectTemplates();
			model.addAttribute("tpList", tmpList);
			return "templateList";
	}
	
}
